/*
    project-bppc
    Copyright (C) 2011  Michal Kóňa

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef CLIST_H
#define CLIST_H

#include <memory.h>

/**
 * CList class
 * CList is a FIFO (First-In-First-Out) data structure.
 *
 * @author Michal Kóňa
 * @version 10/09/2011
 */

namespace Stack
{

template<typename T>

class CList
{
private:
	T **m_ppList;
	int m_iCount;
	int m_iIndex;

	int setSize (int a_iSize) {
		if (a_iSize <= m_iCount) {
			return 0;
		}

		T** ppList = new T* [a_iSize];
		if (ppList == 0)
			return -1;

		if (m_iCount > 0) {
			memcpy (ppList, m_ppList, sizeof (T*) * m_iCount);
			delete [] m_ppList;

		}

		m_iCount = a_iSize;
		m_ppList = ppList;

		return 0;
	}

	void free () {
		delete [] m_ppList;
		m_ppList = 0;
		m_iCount = 0;
	}
protected:
	static int m_iTotalCount;
public:


	CList() : m_iIndex (0) {
		m_iCount = 0;
		m_ppList = 0;
	}
	virtual ~CList() {
		free();
	}
	void push (T * a_pNode) {
		setSize (m_iCount + 1); //32
		m_ppList[m_iCount-1] = a_pNode;
	}

	void push (T a_Node) {
		setSize (m_iCount + 1);
		m_ppList[m_iCount-1] = new T (a_Node);
	}

	inline T* pop () {
		if (m_iCount > 0) {
			T *pTmp = 0;
			pTmp = m_ppList[0];
			memmove (&m_ppList[0], &m_ppList[1], sizeof (T*) * (m_iCount - 1));
			m_iCount--;
			return pTmp;
		}
		return 0;
	}

	inline int getCount() const {
		return m_iCount;
	}

	inline int getIndex() const {
		return m_iIndex;
	}

	static int TotalCount() {
		return (m_iTotalCount);
	}

};

template<typename T> int CList<T>::TotalCount() = 0;

}

#endif // CLIST_H
